package com.freshmarket.freshmarket.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freshmarket.freshmarket.entity.LimitedTimeOffer;
import com.freshmarket.freshmarket.dao.LimitedTimeOfferMapper;
import com.freshmarket.freshmarket.entity.ProductType;
import com.freshmarket.freshmarket.entity.form.KillProduct;
import com.freshmarket.freshmarket.service.ILimitedTimeOfferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@Service
public class LimitedTimeOfferServiceImpl extends ServiceImpl<LimitedTimeOfferMapper, LimitedTimeOffer> implements ILimitedTimeOfferService {
    @Autowired
    private LimitedTimeOfferMapper limitedTimeOfferMapper;
    @Override
    public List<ProductType> findProductType() {
        return  limitedTimeOfferMapper.findProductType();
    }



    @Override
    public IPage<KillProduct> pageKillProduct(int pageIdx, int pageSize, int id) throws ParseException {
        Date date = new Date();
        List<Integer> byProductTypeId = limitedTimeOfferMapper.findByProductTypeId(id);
        IPage<KillProduct> killProductIPage;

        if("".equals(byProductTypeId)||byProductTypeId.size()==0){
                return null;
        }else {
            Page<KillProduct> page = new Page<>(pageIdx,pageSize);
            killProductIPage  = limitedTimeOfferMapper.pageKillProduct(page, byProductTypeId);
            if(killProductIPage.getRecords()!=null && ! ("").equals(killProductIPage.getRecords())) {
                for (int i = 0; i < killProductIPage.getRecords().size(); i++) {
                    String limitedTimeOfferStartTime = killProductIPage.getRecords().get(i).getLimitedTimeOfferStartTime();

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    Date parse = sdf.parse(limitedTimeOfferStartTime);


                    //time 代表还有多少时间  商品才能开始购买
                    long time = (parse.getTime() - date.getTime()) > 0 ? (parse.getTime() - date.getTime()) : 0;

                    killProductIPage.getRecords().get(i).setLimitedTimeOfferStartTime(time + "");
                    System.out.println(killProductIPage.getRecords().get(i).getLimitedTimeOfferStartTime());
                    String limitedTimeOfferDuration = killProductIPage.getRecords().get(i).getLimitedTimeOfferDuration();

                    double parseDouble = Double.parseDouble(limitedTimeOfferDuration);
                    System.out.println("数据库的剩余时间"+parseDouble);
                    //times  持续多久 这个活动
                    double times = parseDouble * 60 * 60 * 1000;
                    System.out.println("数据库的转换毫秒"+times);
                    //end  剩余时间
                    long end= (long)((parse.getTime()+times-date.getTime())>0?times:0);
                    System.out.println(end);

                    long t=(long)times;
                    //活动未开始  剩余时间
                    String formatTime1="";
                    //活动开始了  剩余时间
                    String formatTime2="";
                    //默认的
                    String dataTime="";

                    if(parse.getTime() - date.getTime()>0){
                        formatTime1 = Utils.getFormatTime(parse.getTime() - date.getTime());
                        //未开始  传持久时间
                        dataTime="活动倒计时:"+formatTime1;
                        killProductIPage.getRecords().get(i).setLimitedTimeOfferDuration(String.valueOf(times));
                    } else if(parse.getTime() - date.getTime()+t>0){
                        formatTime2= Utils.getFormatTime(parse.getTime() - date.getTime()+t);
                        //开始了 传现在 还剩多久时间
                        killProductIPage.getRecords().get(i).setLimitedTimeOfferDuration(String.valueOf(parse.getTime() - date.getTime()+t));
                        dataTime="剩余时间"+formatTime2;
                    }else{

                        killProductIPage.getRecords().get(i).setLimitedTimeOfferDuration(String.valueOf(0));
                        dataTime="活动结束了";
                    }

                    killProductIPage.getRecords().get(i).setNowData(formatTime2);
                    killProductIPage.getRecords().get(i).setDataTime(dataTime);
                }
            }
        }

        return killProductIPage;
    }

    @Override
    public List<String> Address() {
        return limitedTimeOfferMapper.Address() ;
    }
}
